package com.he1618.boot3.app;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.devops20210625.AsyncClient;
import com.aliyun.sdk.service.devops20210625.models.*;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class YunXiaoApp {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                // Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
                .accessKeyId("LTAI5tQJ2pzB4CWX8vLJQNMg")
                .accessKeySecret("I8AHyzyuTqkxVotNtLLViMkWRGi32c")
                //.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token
                .build());

        // Configure the Client
        AsyncClient client = AsyncClient.builder()
                //.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
                .credentialsProvider(provider)
                //.serviceConfiguration(Configuration.create()) // Service-level configuration
                // Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                // Endpoint 请参考 https://api.aliyun.com/product/devops
                                .setEndpointOverride("devops.cn-hangzhou.aliyuncs.com")
                        //.setConnectTimeout(Duration.ofSeconds(30))
                )
                .build();



        // Parameter settings for API request
        ListOrganizationsRequest skipPipelineJobRunRequest = ListOrganizationsRequest.builder()
                // Request-level configuration rewrite, can set Http request parameters, etc.
                // .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
                .build();

        // Asynchronously get the return value of the API request
        CompletableFuture<ListOrganizationsResponse> response = client.listOrganizations(skipPipelineJobRunRequest);
        // Synchronously get the return value of the API request
        ListOrganizationsResponse resp = response.get();
        List<ListOrganizationsResponseBody.Result> ls = resp.getBody().getResult();
        for (ListOrganizationsResponseBody.Result l : ls) {
            ListProjectsRequest listProjectsRequest=ListProjectsRequest.builder().organizationId(l.getOrganizationId()).category("Project").build();
            CompletableFuture<ListProjectsResponse> projects = client.listProjects(listProjectsRequest);
            System.out.println(new Gson().toJson(projects.get()));

        }
        System.out.println(new Gson().toJson(resp));
        // Asynchronous processing of return values
        /*response.thenAccept(resp -> {
            System.out.println(new Gson().toJson(resp));
        }).exceptionally(throwable -> { // Handling exceptions
            System.out.println(throwable.getMessage());
            return null;
        });*/

        // Finally, close the client
        client.close();
    }
}
